package com.changgou.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.pojo.Brand;
import com.changgou.service.BrandService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author : 残影
 * @Date: 2020-07-28 17:03
 */
@RestController
@RequestMapping("/brand")
@Api(tags = "品牌模块")
public class BrandController {
    @Autowired
    private BrandService brandService;

    @ApiOperation("查询全部数据")
    @GetMapping
    public Result<Brand> findAll(){
        List<Brand> brandList = brandService.findAll();
        return new Result(true, StatusCode.OK,"查询成功",brandList) ;
    }

    @ApiOperation("根据ID查询品牌数据")
    @GetMapping("/{id}")
    public Result<Brand> findById(@PathVariable Integer id){
        //根据ID查询
        Brand brand = brandService.findById(id);
        return new Result(true,StatusCode.OK,"查询成功",brand);
    }

    @ApiOperation("新增品牌数据")
    @PostMapping
    public Result add(@RequestBody Brand brand){
        brandService.add(brand);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    @ApiOperation("修改品牌数据")
    @PutMapping(value="/{id}")
    public Result update(@RequestBody Brand brand,@PathVariable Integer id){
        //设置ID
        brand.setId(id);
        //修改数据
        brandService.update(brand);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    @ApiOperation("根据ID删除品牌数据")
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        brandService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    @ApiOperation("多条件搜索品牌数据")
    @PostMapping(value = "/search" )
    public Result findList(@RequestBody(required = false) Brand brand){
        List<Brand> list = brandService.findList(brand);
        return new Result(true,StatusCode.OK,"查询成功",list);
    }

    @ApiOperation("分页搜索实现")
    @GetMapping(value = "/search/{page}/{size}" )
    public Result findPage(@PathVariable  int page, @PathVariable  int size){
        //分页查询
        PageInfo<Brand> pageInfo = brandService.findPage(page, size);
        return new Result(true,StatusCode.OK,"查询成功",pageInfo);
    }

    @ApiOperation("分页搜索实现")
    @PostMapping(value = "/search/{page}/{size}" )
    public Result findPage(@RequestBody(required = false) Brand brand, @PathVariable  int page, @PathVariable  int size){
        //执行搜索
        PageInfo<Brand> pageInfo = brandService.findPage(brand, page, size);
        return new Result(true,StatusCode.OK,"查询成功",pageInfo);
    }

    @ApiOperation("根据分类id查询品牌数据")
    @GetMapping("category/{categoryId}")
    public Result findByCategory(@PathVariable Integer categoryId){
        List<Brand> brandList = brandService.findByCategory(categoryId);
        return new Result(true,StatusCode.OK,"查询成功",brandList);
    }
}
